package org.akushe.core.repository.workspace;

import javax.jcr.Repository;
import javax.jcr.RepositoryException;

import org.akushe.core.constant.CommonConstant;
import org.akushe.core.repository.IRepositoryService;
import org.akushe.core.repository.ManagedSessionFactory;
import org.akushe.core.repository.RepositoryServiceImpl;
import org.akushe.core.security.CredentialFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class WorkspaceController {

	private static WorkspaceController controller;
	
	private ManagedSessionFactory sessionFactory;
	
    /** logger */
    private static final Logger log = LoggerFactory.getLogger(WorkspaceController.class);
    
	
	private WorkspaceController(){
		this.init();
	}
	private void init(){
		log.debug("Initialaizing Workspace controller.");
		IRepositoryService service = new RepositoryServiceImpl();
		try{
			log.debug("Initialiazing repository.");
			service.initRepository();
			Repository repository = service.getRepository();
			log.debug("Repository loaded successfully.");
			
			this.sessionFactory = new ManagedSessionFactory(repository, CredentialFactory.createCredential(CommonConstant.SIMPLE_CREDENTIAL));
		}
		catch (RepositoryException e) {
			log.error("Error occured during loading repository.");
			log.error(e.getMessage(), e);
		}
		catch(Exception ex){
			log.error("Error occured during initialiazing WorkspaceController");
			log.error(ex.getMessage(),ex);
		}
	}
	public static WorkspaceController getInstance(){
		if(controller == null){
			controller = new WorkspaceController();
		}
		return controller;
	}
	public ManagedSessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	
}
